﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace HangerSupport
{
	public class ReverSeismicComponent
	{
		public ReverSeismicComponent(Autodesk.Revit.DB.Document doc, DlgHsSeiComRev dlg)
		{
			this.dlg = dlg;
			this.doc = doc;
			this.uiDoc = new UIDocument(doc);
		}

		public void Execute()
		{
			this.tran = new Transaction(this.doc, "抗震组件添加");
			this.LoadFamilySymbol();
			for (;;)
			{
				Autodesk.Revit.DB.View activeView = this.doc.ActiveView;
				this.dlg.CmdEsc = true;
				try
				{
					bool flag = this.fs == null;
					if (flag)
					{
						break;
					}
					List<Element> elems = this.GetElems();
					this.adjustType = this.dlg.AdjustType;
					bool flag2 = elems == null || elems.Count < 1;
					if (!flag2)
					{
						this.tran.Start();
						int num = 0;
						foreach (Element element in elems)
						{
							SeismicComponentModel hangerSeiComModel = this.GetHangerSeiComModel(element);
							this.SetInformationByAdjustedType(this.adjustType, ref hangerSeiComModel);
							this.asc = new AddSeismicComponent(this.doc, hangerSeiComModel, this.fs);
							num += this.asc.addSeismicComponents(new List<Element>
							{
								element
							});
						}
						bool flag3 = num > 0;
						if (flag3)
						{
							YJKMessageBox.Information(string.Format("由于布置高度过小，造成 {0} 个组件无法正常布置！", num));
						}
						this.tran.Commit();
					}
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
				{
					Autodesk.Revit.DB.View activeView2 = this.doc.ActiveView;
					this.exMessage = ex.ToString();
					bool flag4 = RevitVersionFuncs.GetViewName(activeView) == RevitVersionFuncs.GetViewName(activeView2);
					if (flag4)
					{
						break;
					}
				}
				catch (Exception ex2)
				{
					this.exMessage = ex2.ToString();
					bool cmdEsc = this.dlg.CmdEsc;
					if (cmdEsc)
					{
						break;
					}
				}
				finally
				{
					bool flag5 = this.tran != null && (int)this.tran.GetStatus() != 3 && (int)this.tran.GetStatus() != 2;
					if (flag5)
					{
						this.tran.RollBack();
					}
				}
			}
		}

		private List<Element> GetElems()
		{
			List<Element> result;
			try
			{
				result = this.uiDoc.Selection.PickElementsByRectangle(new FilterElemsByFamilySymbolName(this.GetLisFilterFamilyName()), "请框选抗震组件需要换向的支吊架...").ToList<Element>();
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
			{
				throw ex;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private List<string> GetLisFilterFamilyName()
		{
			List<string> list = new List<string>();
			for (int i = 1; i < 16; i++)
			{
				list.Add(i + "层吊架");
			}
			list.Add("钢筋吊架");
			list.Add("单管吊架");
			return list;
		}

		private void LoadFamilySymbol()
		{
			try
			{
				this.tran.Start();
				string text = "";
				this.fs = YJKRevitTools.GetFamilySymbolByName("SeismicComponent", "侧向支撑", this.doc, ref text);
				this.tran.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				this.fs = null;
			}
		}

		private SeismicComponentModel GetHangerSeiComModel(Element elem)
		{
			SeismicComponentModel result = new SeismicComponentModel();
			this.DeleteLastTimeAddSeiComs(elem, ref result);
			return result;
		}

		private void DeleteLastTimeAddSeiComs(Element hanger, ref SeismicComponentModel seiComModel)
		{
			Guid guid = new Guid("BBEC5246-99AA-45B9-A1B1-819109282C3B");
			bool flag = Schema.Lookup(guid) == null;
			if (!flag)
			{
				Entity entity = hanger.GetEntity(Schema.Lookup(guid));
				bool flag2 = entity.Schema == null;
				if (!flag2)
				{
					string tagIds = entity.Get<string>(Schema.Lookup(guid).GetField("AddSeismicComponents"));
					this.GetInfomation(hanger, tagIds, ref seiComModel);
				}
			}
		}

		private void GetInfomation(Element hanger, string tagIds, ref SeismicComponentModel seiComModel)
		{
			List<Element> list = new List<Element>();
			char[] separator = new char[]
			{
				'@'
			};
			string[] array = tagIds.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries);
			string[] array2 = array;
			int i = 0;
			while (i < array2.Length)
			{
				string s = array2[i];
				int num;
				bool flag = int.TryParse(s, out num);
				if (flag)
				{
					ElementId elementId = new ElementId(num);
					Element element = this.doc.GetElement(elementId);
					try
					{
						bool flag2 = element != null;
						if (flag2)
						{
							list.Add(element);
						}
					}
					catch (Exception)
					{
					}
				}
				IL_78:
				i++;
				continue;
				goto IL_78;
			}
			bool flag3 = list.Count < 1;
			if (!flag3)
			{
				this.GetSeiComsInfomation(hanger, list, ref seiComModel);
			}
		}

		private void GetSeiComsInfomation(Element hanger, List<Element> elems, ref SeismicComponentModel seiComModel)
		{
			seiComModel.hsType = this.GetHangerType(hanger);
			Element element = elems.FirstOrDefault<Element>();
			bool flag = element == null;
			if (!flag)
			{
				Parameter parameter = element.GetParameters("斜撑b").FirstOrDefault<Parameter>();
				bool flag2 = parameter != null && parameter.HasValue;
				if (flag2)
				{
					seiComModel.widthB = parameter.AsDouble() * 304.8;
				}
				Parameter parameter2 = element.GetParameters("斜撑h").FirstOrDefault<Parameter>();
				bool flag3 = parameter2 != null && parameter2.HasValue;
				if (flag3)
				{
					seiComModel.hightH = parameter2.AsDouble() * 304.8;
				}
				Parameter parameter3 = element.GetParameters("斜撑t").FirstOrDefault<Parameter>();
				bool flag4 = parameter3 != null && parameter3.HasValue;
				if (flag4)
				{
					seiComModel.thickT = parameter3.AsDouble() * 304.8;
				}
				List<ElementId> list = new List<ElementId>();
				foreach (Element element2 in elems)
				{
					list.Add(element2.Id);
					Parameter parameter4 = element2.GetParameters("位置标识").FirstOrDefault<Parameter>();
					Parameter parameter5 = element2.GetParameters("侧向支撑角度").FirstOrDefault<Parameter>();
					bool flag5 = parameter4 != null && parameter4.HasValue && parameter5 != null && parameter5.HasValue;
					if (flag5)
					{
						string text = parameter4.AsString();
						string a = text;
						if (!(a == "A"))
						{
							if (!(a == "B"))
							{
								if (!(a == "C"))
								{
									if (!(a == "D"))
									{
										if (!(a == "E"))
										{
											if (a == "F")
											{
												seiComModel.location_F_Angle = new KeyValuePair<string, double>("Location_F", Convert.ToDouble(parameter5.AsDouble() * 180.0 / Math.PI));
											}
										}
										else
										{
											seiComModel.location_E_Angle = new KeyValuePair<string, double>("Location_E", Convert.ToDouble(parameter5.AsDouble() * 180.0 / Math.PI));
										}
									}
									else
									{
										seiComModel.location_D_Angle = new KeyValuePair<string, double>("Location_D", Convert.ToDouble(parameter5.AsDouble() * 180.0 / Math.PI));
									}
								}
								else
								{
									seiComModel.location_C_Angle = new KeyValuePair<string, double>("Location_C", Convert.ToDouble(parameter5.AsDouble() * 180.0 / Math.PI));
								}
							}
							else
							{
								seiComModel.location_B_Angle = new KeyValuePair<string, double>("Location_B", Convert.ToDouble(parameter5.AsDouble() * 180.0 / Math.PI));
							}
						}
						else
						{
							seiComModel.location_A_Angle = new KeyValuePair<string, double>("Location_A", Convert.ToDouble(parameter5.AsDouble() * 180.0 / Math.PI));
						}
					}
				}
				this.doc.Delete(list);
			}
		}

		private HSType GetHangerType(Element elem)
		{
			HSType result = HSType.kZJ_XB;
			FamilyInstance familyInstance = elem as FamilyInstance;
			bool flag = familyInstance != null;
			if (flag)
			{
				string name = familyInstance.Symbol.Name;
				bool flag2 = name.Contains("层吊架");
				if (flag2)
				{
					result = HSType.kDJ_DC;
				}
				else
				{
					bool flag3 = name.Contains("钢筋吊架");
					if (flag3)
					{
						result = HSType.kDJ_GJ;
					}
					else
					{
						bool flag4 = name.Contains("单管吊架");
						if (flag4)
						{
							result = HSType.kDJ_DG;
						}
					}
				}
			}
			return result;
		}

		private void SetInformationByAdjustedType(AdjustedType adjustType, ref SeismicComponentModel seiComModel)
		{
			switch (seiComModel.hsType)
			{
			case HSType.kDJ_DC:
			case HSType.kDJ_GJ:
				this.SetGjDcInformation(adjustType, ref seiComModel);
				break;
			case HSType.kDJ_DG:
				this.SetDgInformation(adjustType, ref seiComModel);
				break;
			}
		}

		private void SetGjDcInformation(AdjustedType adjustType, ref SeismicComponentModel seiComModel)
		{
			SeismicComponentModel seismicComponentModel = seiComModel.DeepCopy();
			switch (adjustType)
			{
			case AdjustedType.Rotate:
				seiComModel.location_A_Angle = seismicComponentModel.location_B_Angle;
				seiComModel.location_B_Angle = seismicComponentModel.location_A_Angle;
				seiComModel.location_C_Angle = seismicComponentModel.location_F_Angle;
				seiComModel.location_F_Angle = seismicComponentModel.location_C_Angle;
				seiComModel.location_D_Angle = seismicComponentModel.location_E_Angle;
				seiComModel.location_E_Angle = seismicComponentModel.location_D_Angle;
				break;
			case AdjustedType.AlongPipeMirror:
				seiComModel.location_A_Angle = seismicComponentModel.location_B_Angle;
				seiComModel.location_B_Angle = seismicComponentModel.location_A_Angle;
				seiComModel.location_C_Angle = seismicComponentModel.location_D_Angle;
				seiComModel.location_D_Angle = seismicComponentModel.location_C_Angle;
				seiComModel.location_E_Angle = seismicComponentModel.location_F_Angle;
				seiComModel.location_F_Angle = seismicComponentModel.location_E_Angle;
				break;
			case AdjustedType.SameSideMirror:
				seiComModel.location_C_Angle = seismicComponentModel.location_E_Angle;
				seiComModel.location_E_Angle = seismicComponentModel.location_C_Angle;
				seiComModel.location_D_Angle = seismicComponentModel.location_F_Angle;
				seiComModel.location_F_Angle = seismicComponentModel.location_D_Angle;
				break;
			}
		}

		private void SetDgInformation(AdjustedType adjustType, ref SeismicComponentModel seiComModel)
		{
			SeismicComponentModel seismicComponentModel = seiComModel.DeepCopy();
			switch (adjustType)
			{
			case AdjustedType.Rotate:
				seiComModel.location_A_Angle = seismicComponentModel.location_B_Angle;
				seiComModel.location_B_Angle = seismicComponentModel.location_A_Angle;
				seiComModel.location_C_Angle = seismicComponentModel.location_D_Angle;
				seiComModel.location_D_Angle = seismicComponentModel.location_C_Angle;
				break;
			case AdjustedType.AlongPipeMirror:
				seiComModel.location_A_Angle = seismicComponentModel.location_B_Angle;
				seiComModel.location_B_Angle = seismicComponentModel.location_A_Angle;
				break;
			case AdjustedType.SameSideMirror:
				seiComModel.location_C_Angle = seismicComponentModel.location_D_Angle;
				seiComModel.location_D_Angle = seismicComponentModel.location_C_Angle;
				break;
			}
		}

		private DlgHsSeiComRev dlg;

		private Document doc;

		private UIDocument uiDoc;

		private Transaction tran;

		private FamilySymbol fs;

		private string exMessage;

		private AddSeismicComponent asc;

		private AdjustedType adjustType;
	}
}
